﻿@using Aguacongas.TheIdServer.BlazorApp.Pages.RelyingParty.Components

@page "/relyingparty"
@page "/relyingparty/{id}"

@inherits EntityModel<Entity.RelyingParty>
@inject System.Net.Http.IHttpClientFactory _factory

@if (Model == null)
{
    <PageLoading />
}
else
{
    <EditForm EditContext="EditContext" OnValidSubmit="HandleValidSubmit">
        <CascadingValue Value="HandleModificationState">
            <div class="row bg-light sticky-top">
                <div class="col">
                    @if (IsNew)
                    {
                        <AuthorizeText Id="name" Placeholder="@Localizer["identity id"]" @bind-Value="@Model.Id" />
                    }
                    else
                    {
                        <h3>@Id</h3>
                    }
                </div>
                <div class="col">
                    @if (!NonEditable)
                    {
                        <SaveButton />
                        @if (!IsNew)
                        {
                            <DeleteEntityButton EntityId="@Model.Id" DeleteConfirmed="DeleteEntity" />
                        }
                    }
                    @if (!IsNew)
                    {
                        <ExportButton EntityPath="@EntityPath" Request="@ExportRequest" />
                        <CloneButton />
                    }
                </div>
                <div class="col">
                    <Filter CssClass="mt-1" />
                </div>
                <div class="w-100"></div>
                <div class="col">
                    <ul class="list-inline text-lowercase">
                        <li class="list-inline-item">
                            <a href="@(NavigationManager.Uri)#claims" @onclick='() => ScrollTo("mappings")' @onclick:preventDefault>@Localizer["claims mapping"]</a>
                        </li>
                    </ul>
                </div>
                <div class="w-100"></div>
                <FluentValidationValidator />
                <ValidationSummary class="col" />
            </div>
            <div class="entity-details">
                <div class="card mb-3">
                    <div class="card-body">
                        <div class="mb-3 row">
                            <label class="col col-form-label" for="description">
                                @Localizer["decription"]
                            </label>
                            <div class="col-lg-10 col-sm-12">
                                <AuthorizeTextArea Id="description" Placeholder="@Localizer["description"]" @bind-Value="@Model.Description" MaxLength="2000" />
                            </div>
                        </div>
                        <div class="mb-3 row">
                            <label class="col col-form-label">
                                @Localizer["token type"]
                            </label>
                            <div class="col-lg-10 col-sm-12">
                                <AuthorizeDropDownButton @bind-Value="@Model.TokenType" Values="TOKENTYPELIST" />
                            </div>
                        </div>
                        <div class="mb-3 row">
                            <label class="col col-form-label">
                                @Localizer["signature algorithm"]
                            </label>
                            <div class="col-lg-10 col-sm-12">
                                <AuthorizeDropDownButton @bind-Value="@Model.SignatureAlgorithm" Values="SIGNATUREALGORITHMLIST" />
                            </div>
                        </div>
                        <div class="mb-3 row">
                            <label class="col col-form-label">
                                @Localizer["signature digest"]
                            </label>
                            <div class="col-lg-10 col-sm-12">
                                <AuthorizeDropDownButton @bind-Value="@Model.DigestAlgorithm" Values="DIGESTLIST" />
                            </div>
                        </div>
                        <div class="mb-3 row">
                            <label class="col col-form-label">
                                @Localizer["name id format"]
                            </label>
                            <div class="col-lg-10 col-sm-12">
                                <AuthorizeDropDownButton @bind-Value="@Model.SamlNameIdentifierFormat" Values="NAMEIDENTIFIERFORMATLIST" />
                            </div>
                        </div>
                        <div class="mb-3 row">
                            <label class="col col-form-label">
                                @Localizer["encryption certificate"]
                            </label>
                            <div class="col-lg-10 col-sm-12">                                
                                <div class="row">
                                    <div class="col form-control-plaintext">
                                        @if (_thumbprint != null)
                                        {
                                            foreach(var s in _thumbprint)
                                            {
                                                <div>@s</div>
                                            }
                                        }
                                        else
                                        {
                                            <InputFile OnChange="@SetCertificateAsync" accept=".cer,.pem,.p7b" />
                                        }
                                    </div>
                                    <div class="col-1">
                                        @if (Model.EncryptionCertificate != null)
                                        {
                                            <AuthorizeButton Type="button" CssSubClass="btn-primary" Clicked="@RemoveCertificate">
                                                <span class="oi oi-trash"></span>
                                            </AuthorizeButton>                                    
                                        }
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div id="mappings" class="card mb-3">
                    <div class="card-header">
                        <EntitySubGridTitle Text="@Localizer["Claims mapping"]" Collection="@Model.ClaimMappings" CreateModel="@CreateMapping" />
                    </div>
                    <div class="card-body">
                        <ClaimMappings Collection="@Model.ClaimMappings" />
                    </div>
                </div>
            </div>
        </CascadingValue>
    </EditForm>
}
